package com.qf.device.controller;

import cn.hutool.core.util.IdUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.digest.MD5;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.qf.device.entity.Device;
import com.qf.device.entity.DeviceTopic;
import com.qf.device.service.DeviceTopicService;
import com.qf.product.entity.Product;
import com.qf.product.service.ProductService;
import com.qf.dto.PageDto;
import com.qf.device.service.DeviceService;
import com.qf.tenant.entity.Tenant;
import com.qf.vo.PageResult;
import com.qf.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 设备表 前端控制器
 * </p>
 *
 * @author 
 * @since 2023-10-26
 */

@CrossOrigin
@RestController
@RequestMapping("/device")
public class DeviceController {

    @Autowired
    private DeviceService deviceService;

    @Autowired
    private ProductService productService;

    @Autowired
    private DeviceTopicService deviceTopicService;


    /*分页查询*/
    @PostMapping("page")
    public PageResult<Device> page(@RequestBody PageDto<Device> pageDto){
        return deviceService.pageInfo(pageDto);
    }

    /*查询所有的数据*/
    @PostMapping("all")
    public PageResult<Device> findAll(@RequestParam Long productId,@RequestBody PageDto<Device> pageDto){
        return deviceService.findAll(productId,pageDto);
    }

    /*查询所有的产品name和id*/
    @PostMapping("productAll")
    public List<Product> findProductNameAndId(){
        return productService.list();
    }

    /*查询设备详情*/
    @PostMapping("details")
    public Result details(@RequestParam Long id){
        return deviceService.findOne(id);
    }

    /*查询单个设备所对应的产品信息*/
    @PostMapping("findProduct")
    public Result findProduct(@RequestParam Long id){
        return productService.findOneByDeviceId(id);
    }

    /*查询单个设备对应的topic信息*/
    @PostMapping("findTopic")
    public Result findTopic(@RequestParam String deviceKey){
        return deviceTopicService.findByDeviceKey(deviceKey);
    }

    /*删除一个设备*/
    @PostMapping("delete")
    public Result delete(@RequestParam Long id){
        if(deviceService.removeById(id)){
            return new Result(0,"success");
        }else{
            return new Result(1,"fail");
        }
    }

    /*增加新设备*/
    @PostMapping("add")
    public Result add(@RequestBody Device device){
        String idStr = IdWorker.getIdStr();
        device.setDeviceKey("device_" + idStr);
        device.setDeviceSecret(SecureUtil.md5(idStr));
        device.setCreateTime(new Date());
        device.setUpdateTime(new Date());
        device.setDeleted(0);
        Tenant tenant = (Tenant) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        device.setTenantId(tenant.getId());
        device.setGroupId(0L);
        device.setActive(1);

        //创建设备的两个topic(数据上传和发送指令)
        DeviceTopic dataTopic = new DeviceTopic();
        dataTopic.setTopic("/qfjava/device/data/" + device.getDeviceKey());
        dataTopic.setDeviceKey(device.getDeviceKey());
        dataTopic.setDeleted(0);
        dataTopic.setCreateTime(new Date());
        dataTopic.setUpdateTime(new Date());

        DeviceTopic cmdTopic = new DeviceTopic();
        cmdTopic.setTopic("/qfjava/device/ctrl/" + device.getDeviceKey());
        cmdTopic.setDeviceKey(device.getDeviceKey());
        cmdTopic.setDeleted(0);
        cmdTopic.setCreateTime(new Date());
        cmdTopic.setUpdateTime(new Date());

        deviceTopicService.save(dataTopic);
        deviceTopicService.save(cmdTopic);

        if(deviceService.save(device)){
            return new Result(0,"success");
        }
        return new Result(1,"fail");
    }

    /*更改状态*/
    @PostMapping("instruction")
    public Result instruction(@RequestBody Device device){
        if(deviceService.updateById(device)){
            return new Result(0,"success");
        }else{
            return new Result(1,"fail");
        }
    }
}
